/*
 * Copyright (c) 2010 Nokia Corporation.
 */

#include <QTimerEvent>
#include "PictureLoaderThread.h"
#include <QDebug>
#include "mainwindow.h"

PictureLoaderThread::PictureLoaderThread(QObject *parent) :
    QThread(parent)
{
    start(IdlePriority);
}

PictureLoaderThread::~PictureLoaderThread()
{
    m_loadingCache.clear();
}

QSize PictureLoaderThread::imageScaledSize(const QSize& preferredSize, QString file)
{
    // Set image name
    QImageReader* reader = new QImageReader();
    reader->setFileName(file);
    // Set/scale image size into preferred size
    QSize imageSize = reader->size();
    imageSize.scale(preferredSize, Qt::KeepAspectRatio);
    delete reader;
    return imageSize;
}

void PictureLoaderThread::loadImages(QList<PictureData*>& data)
{
    QMutexLocker locker(&m_mutex);

    m_loadingCache << data;

    m_waitCondition.wakeAll();
}


void PictureLoaderThread::loadImage(PictureData& d)
{
    QMutexLocker locker(&m_mutex);

    m_loadingCache.append(&d);

    m_waitCondition.wakeAll();
}

void PictureLoaderThread::run()
{
    forever {

        while(!m_loadingCache.isEmpty()) {
            PictureData* d = m_loadingCache.takeFirst();

            if(d->image)
            {
                // Read image
                QImageReader* reader = new QImageReader();
                // Set image name
                reader->setFileName(d->path);

                QSize imageSize = reader->size();

                if(false == reader->read(d->image))
                {
                    delete reader;
                    // Read image
                    reader = new QImageReader();
                    // Set image name
                    reader->setFileName(d->path);
                    QSize tSize = imageSize;
                    tSize.scale(imageSize*0.8, Qt::KeepAspectRatio);
                    reader->setScaledSize(tSize);
                    if(false == reader->read(d->image))
                    {
                        delete reader;
                        // Read image
                        reader = new QImageReader();
                        // Set image name
                        reader->setFileName(d->path);
                        QSize tSize = imageSize;
                        tSize.scale(imageSize*0.5, Qt::KeepAspectRatio);
                        reader->setScaledSize(tSize);
                        if(false == reader->read(d->image))
                        {
                            delete reader;
                            // Read image
                            reader = new QImageReader();
                            // Set image name
                            reader->setFileName(d->path);
                            QSize tSize = imageSize;
                            tSize.scale(imageSize*0.25, Qt::KeepAspectRatio);
                            qDebug()<<tSize.width()<<","<<tSize.height();
                            reader->setScaledSize(tSize);
                            reader->read(d->image);
                        }
                    }
                }
                delete reader;
                d->imageLoaded = true;
            }
            if(d->p.isNull())
            {
                // Read image
                QImageReader* reader = new QImageReader();
                // Set image name
                reader->setFileName(d->path);
                // Set/scale image size into screen
                QSize imageSize = reader->size();
                imageSize.scale(d->size.toSize(), Qt::KeepAspectRatio);

                reader->setScaledSize(imageSize);
                reader->setQuality(1);
                // Read image
                QImage* image = new QImage();
                reader->read(image);
                delete reader;

                m_mutex.lock();
                emit imageLoaded(image, d);
                m_mutex.unlock();
            }
        }

        m_mutex.lock();
        m_waitCondition.wait(&m_mutex);
        m_mutex.unlock();
    }
}

